package com.lyj.controller;


import com.lyj.pojo.Admin;
import com.lyj.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpSession;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;

@Controller
public class AdminController {

    @Autowired
    AdminService adminService;

    @RequestMapping("/queryAdminAll")
    @ResponseBody
    public List<Admin> queryAdminAll() {
        List<Admin> admins = adminService.queryAdminAll();
        return admins;

    }

    @RequestMapping("/addAdmin")
    @ResponseBody
    public Boolean addAdmin(Admin admin) {

        String imageToBase64 = ImageToBase64("target/classes/static/base64.jpg");
        admin.setImg(imageToBase64);
        admin.setDuty("管理员");
        Boolean aBoolean = adminService.addAdmin(admin);
        return aBoolean;

    }

    //删除管理员
    @RequestMapping("/delAdminById")
    @ResponseBody
    public Boolean delAdminById(int adminId) {

        Boolean aBoolean = adminService.delAdminById(adminId);
        return aBoolean;
    }

    //修改管理员信息
    @RequestMapping("/updateAdminById")
    @ResponseBody
    public Boolean updateAdminById(String base64Data, Admin admin) {

        if (base64Data == null || "".equals(base64Data)) {
            String imageToBase64 = ImageToBase64("target/classes/static/base64.jpg");
            //System.out.println(imageToBase64);
            admin.setImg(imageToBase64);

            Boolean aBoolean = adminService.updateAdminById(admin);
            return aBoolean;
        } else {
            admin.setImg(base64Data);
            Boolean aBoolean = adminService.updateAdminById(admin);

            return aBoolean;
        }
    }


    //转换base64
    private static String ImageToBase64(String imgPath) {
        byte[] data = null;
        // 读取图片字节数组
        try {
            InputStream in = new FileInputStream(imgPath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        // 返回Base64编码过的字节数组字符串
        String base64 = encoder.encode(Objects.requireNonNull(data));
        System.out.println("本地图片转换Base64:" + base64);
        return base64;
    }
    @RequestMapping("/queryAdminById")
    @ResponseBody
    public Admin queryAdminById(int adminId){

        System.out.println("-------adminId--------"+adminId);
        Admin admin = adminService.queryAdminById(adminId);
        System.out.println("-------admin--------"+admin);
        return admin;
    }


}